import * as _ from 'lodash'
import * as Libs from '../utils/Libs'
import screen from '../utils/screen'
import { post } from '../utils/fetch'
import { getLineAndMoney } from '../utils/api'

export const analysePie = (passVal =  []) => {
  let _legendData = []
  const _seriesData = []
  _.map(passVal, (v) => {
    _legendData.push(v.codeName)
    _seriesData.push({
      value: v.sumMoney,
      name: v.codeName
    })
  })
  return renderPieOptions(_legendData, _seriesData)
}
const renderPieOptions = (legendData, seriesData) => {
  const options = {
    title : {
      text: '',
      subtext: '',
      x:'center'
    },
    tooltip : {
      trigger: 'item',
      formatter: "{a} <br/>{b} : {c} ({d}%)",
      position: ['30%', '30%']
    },
    legend: {
      bottom: '0',
      data: legendData
    },
    grid: {
      containLabel: true
    },
    series : [
      {
        name: '',
        type: 'pie',
        radius : '55%',
        center: ['50%', '40%'],
        data:seriesData,
      }
    ]
  };
  return options;
}


export const analyseCylinderHorizen = (title, legends, yAxisDatas, datas) => {
  return renderCylinderOptionsHorizen(title, legends, yAxisDatas, datas)
}

const renderCylinderOptionsHorizen = (title, legends,yAxisDatas, datas) => {
  const _series = []
  _.map(datas, (v, i) => {
    _series.push({
      name: legends[i],
      type: 'bar',
      stack: '总量',
      label: {
        normal: {
          show: true,
          position: 'insideRight'
        }
      },
      data: v
    })
  })
  const option = {
    tooltip : {
      trigger: 'axis',
      axisPointer : {            // 坐标轴指示器，坐标轴触发有效
        type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      },
      position: ['30%', '30%']
    },
    legend: {
      bottom: 0,
      data: legends,
    },
    grid: {
      containLabel: true
    },
    xAxis:  {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: yAxisDatas,
    },
    series: _series
  };
  return option;
}

export const analyseCylinder = (v = [], dw = "万元") => {
  const mainD = v || []
  let _mainD = []
  let _mainDUp = []
  let _xAxisDatas = []
  _.map(mainD, (v) => {
    _xAxisDatas.push(v.codeName)
    _mainD.push(v.sumMoney)
    _mainDUp.push(v.tbSumMoney)
  })
  console.log('_mainD ', _mainD)
  console.log('_mainDUp ', _mainDUp)
  const legends = ['本期', '上期']
  const datas = [{
    name: '本期',
    type: 'bar',
    data: _mainD
  }, {
    name: '上期',
    type: 'bar',
    data: _mainDUp
  }]
  return renderCylinderOptions('', legends, _xAxisDatas, datas, dw)
}

const renderCylinderOptions = (title, legends, xAxisDatas, datas, dw) => {
  const option = {
    title: {
      text: title
    },
    legend: {
      data:legends
    },
    grid: {
      containLabel: true
    },
    xAxis : {
      type: 'category',
      axisLabel: {
        interval: 0,
        rotate: 45,
      },
      data: xAxisDatas
    },
    yAxis: {
      type: 'value',
      name: '单位(' + dw + ')',
    },
    tooltip : {
      trigger: 'axis',
      axisPointer : {            // 坐标轴指示器，坐标轴触发有效
        type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      },
      position: ['30%', '30%']
    },
    series: datas
  };
  return option;
}

export const analyseFlow = (v = {}, dw = "万元") => {
  const xAxisD = v.cs || []
  const totalD = v.total || []
  const times = v.sl
  const bfb = v.bfb || []
  let flowD = []
  for(var i = 1; i <= times; i++) {
    flowD.push(v["a" + i])
  }
  return renderAnalyseFlowOptions('', xAxisD , totalD, flowD, bfb)

}



const renderAnalyseFlowOptions = (title, xAxis, totalD, flowD, bfb) => {
  const series = []
  series.push({
    name: '辅助',
    type: 'bar',
    stack: '总量',
    itemStyle: {
      normal: {
        barBorderColor: 'rgba(0,0,0,0)',
        color: 'rgba(0,0,0,0)'
      },
      emphasis: {
        barBorderColor: 'rgba(0,0,0,0)',
        color: 'rgba(0,0,0,0)'
      }
    },
    data: totalD
  })
  flowD.map((v) => {
    series.push({
      type: 'bar',
      stack: '总量',
      label: {
        normal: {
          show: true,
          position: 'top'
        }
      },
      data: v
    })
  })

  const  option = {
    title: {
      text: title,
    },
    tooltip : {
      trigger: 'axis',
      axisPointer : {            // 坐标轴指示器，坐标轴触发有效
        type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      },
      formatter: function (params) {
        const dataIdx = params[0].dataIndex
        console.log('format params : ', params)
        var tar;
        if (params[1].value != '-') {
          tar = params[1];
        }
        else {
          tar = params[0];
        }
        return tar.name +  '<br/>' + '百分比' + ' : ' +  + bfb[dataIdx];
        ;
      }
    },
    legend: {
      show: false,
      data:['支出','收入']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '20%',
      containLabel: true
    },
    xAxis: {
      type : 'category',
      splitLine: {show:false},
      axisLabel: {
        interval: 0,
        rotate: 45,
      },
      data :  xAxis
    },
    yAxis: {
      type : 'value'
    },
    series: series
  };
  return option;
}

export const analyseLine = (v = [], dw = "") => {
  const lineMain = v || []

  let _lineMain = []
  let _lineMainUp = []
  let _xAxisDatas = []
  _.map(lineMain, (v) => {
    _xAxisDatas.push({
      time: v.yearMoth,
      tb: v.onRise,
      hb: v.linkRise
    })
    _lineMain.push(v.bigSumMoney)
    _lineMainUp.push(v.tbSumMoney)
  })
  const legends = ['本期', '上期']
  const datas = [{
    name: '本期',
    type: 'line',
    data: _lineMain
  }, {
    name: '上期',
    type: 'line',
    data: _lineMainUp
  }]
  return renderChartOptions('', legends, _xAxisDatas, datas, dw)
}

const renderChartOptions = (title, legends, xAxisDatas, datas, unit = "万元") => {
  const option = {
    title: {
      text: title
    },
    legend: {
      data:legends
    },
    grid: {
      containLabel: true
    },
    tooltip : {
      trigger: 'axis',
      position: ['30%', '30%'],
      formatter: function(datas, ticket, callback) {
        const axisDatas = datas[0].name || {}
        let res = axisDatas.time + '<br/>', val;
        for(var i = 0, length = datas.length; i < length; i++) {
          val = datas[i].value;
          res += datas[i].seriesName + '：' + val + '<br/>';
        }
        res += "同比：" + axisDatas.tb + "%" + '<br/>'
        res += "环比：" + axisDatas.hb + "%" +  '<br />'
        return res;
      }
    },
    xAxis: {
      type: 'category',
      data: xAxisDatas,
      axisLabel: {
        formatter: function(value = {}, idx) {
          return value.time || ""
        }
      }
    },
    yAxis: {
      type: 'value',
      name: '单位(' + unit + ')',
    },
    series: datas
  };
  return option;
}

export const analyseEvlenType = (v = {}, dw="万元") => {
  const legends = v.content.map((single) => {
    return single.name
  })
  console.log(legends)
  const datas = v.content || []
  const xAxis = v.date || []
  return renderEvlenTypeOptions('', legends, xAxis, datas)
}

const renderEvlenTypeOptions = (title, legends, xAxis, datas, unit = "万元") => {
  const option = {
    xAxis: {
      type: 'category',
      data: xAxis
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      show: true,
      data: legends
    },
    yAxis: {
      type: 'value'
    },
    series: datas
  };
  return option;
}


export const getCommonLineMoney = async (params = {}) => {
  return post(getLineAndMoney(params), {})
}

export const analyzeDetail = (msg) => {
  const yj = msg.yj || {}
  const yjExist = yj.isExist === 0
  const yjContent = (yj.yjMain || [])[0]

  const money = msg.money || {}
  const moneyExist = money.isExist === 0
  const moneyContent = (money.moneyMain || 0) + "万元"

  const gk = msg.gk || {}
  const gkExist = gk.isExist ===  0
  const gkContent = (gk.gkMain || [])[0]


  const line = msg.line || {}
  const lineExist = line.isExist === 0
  const lineContent = analyseLine(line)

  return {
    yjExist,
    yjContent,
    moneyExist,
    moneyContent,
    gkExist,
    gkContent,
    lineExist,
    lineContent,
  }
}

export const getLinked = (v) => {
  switch(v) {
    default:
      return {
        linkedName:'CommonDetail'
      }
  }
}

const geneObject = (name, title) => {
  return {
    name: name,
    title: title
  }
}

